﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.Coolzon.Common
{
    public class XMLUtil
    {
        public static class Attr
        {
            public static void Set(XmlNode xNode, string name, string value)
            {
                XmlAttribute xAttr = xNode.Attributes[name];
                if (xAttr == null)
                {
                    xAttr = xNode.OwnerDocument.CreateAttribute(name);
                    xNode.Attributes.Append(xAttr);
                }
                xAttr.Value = value;
            }
            public static void SetGuid(XmlNode xNode, string name, Guid value)
            {
                Set(xNode, name, value.ToString());
            }

            public static void SetVersion(XmlNode xNode, string name, Version value)
            {
                Set(xNode, name, value.ToString());
            }
            public static void SetDouble(XmlNode xNode, string name, double value)
            {
                Set(xNode, name, value.ToString());
            }
            public static void SetInt(XmlNode xNode, string name, int value)
            {
                Set(xNode, name, value.ToString());
            }
            public static void SetBool(XmlNode xNode, string name, bool value)
            {
                Set(xNode, name, value.ToString());
            }

            public static string Get(XmlNode node, string name)
            {
                XmlAttribute xAttr = node.Attributes[name];
                if (xAttr == null)
                {
                    return null;
                }
                return xAttr.Value;
            }
            public static Guid GetGuid(XmlNode node, string name)
            {
                string value = Get(node, name);
                if (string.IsNullOrEmpty(value))
                {
                    return Guid.Empty;
                }
                return new Guid(value);
            }
            public static Version GetVersion(XmlNode node, string name)
            {
                string value = Get(node, name);
                if (string.IsNullOrEmpty(value))
                {
                    return new Version(1, 0, 0, 0);
                }
                return Version.Parse(value);
            }
            public static double GetDouble(XmlNode node, string name)
            {
                string value = Get(node, name);
                if (string.IsNullOrEmpty(value))
                {
                    return 0;
                }
                return double.Parse(value);
            }
            public static int GetInt(XmlNode node, string name)
            {
                string value = Get(node, name);
                if (string.IsNullOrEmpty(value))
                {
                    return 0;
                }
                return int.Parse(value);
            }

            public static bool GetBool(XmlNode node, string name)
            {
                string value = Get(node, name);
                if (string.IsNullOrEmpty(value))
                {
                    return false;
                }
                return bool.Parse(value);
            }
            public static bool GetBool(XmlNode node, string name, bool defaultValue)
            {
                string value = Get(node, name);
                if (string.IsNullOrEmpty(value))
                {
                    return defaultValue;
                }
                return bool.Parse(value);
            }
        }

        public static class Element
        {
            public static XmlElement Set(XmlDocument xDocument, string name)
            {
                XmlElement xElement = xDocument.CreateElement(name);
                xDocument.AppendChild(xElement);
                return xElement;
            }

            public static XmlElement Set(XmlElement xParentElement, string name)
            {
                XmlElement xElement = xParentElement.OwnerDocument.CreateElement(name);
                xParentElement.AppendChild(xElement);
                return xElement;
            }
            public static XmlElement Set(XmlElement xParentElement, string name, string value)
            {
                XmlElement xElement = xParentElement.OwnerDocument.CreateElement(name);
                xElement.InnerText = value;
                xParentElement.AppendChild(xElement);
                return xElement;
            }

            public static XmlElement Get(XmlElement xParentElement, string name)
            {
                return xParentElement.SelectSingleNode(name) as XmlElement;
            }
        }
    }
}
